//
//  AEShareInfoView.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2025/6/5.
//  Copyright © 2025 Mr.Wang. All rights reserved.
//

#import "AEShareInfoView.h"
#import "UIWindow+AEShareInfoView.h"

@interface AEShareInfoView ()

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) void(^completionBlock)(id data);
@property (nonatomic, copy) void(^failedBlock)(void);

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIButton *btnCancel;

/// 微信
@property (nonatomic, strong) TYZImageTitleButton *btnWeixin;

/// 朋友圈
@property (nonatomic, strong) TYZImageTitleButton *btnCircle;


/// 微信分享
- (void)initWithBtnWeixin;

/// 朋友圈分享
- (void)initWithBtnCircle;

/// 取消
- (void)initWithBtnCancel;

@end

@implementation AEShareInfoView

- (void)initWithSubView {
    [super initWithSubView];
    
    self.backgroundColor = kWhiteColor;
    CAShapeLayer *shapeLayer = [CALayer bezierPathRound:self.bounds cornerRadii:CGSizeMake(8, 8) roundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    self.layer.mask = shapeLayer;
    
    [self initWithTitleLabel];
    
    /// 微信分享
    [self initWithBtnWeixin];

    /// 朋友圈分享
    [self initWithBtnCircle];

    /// 取消
    [self initWithBtnCancel];

}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(0, 0, self.width, 40);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    self.titleLabel.text = self.title;
}

// ic_share_weixin

// ic_share_wx_circle

/// 微信分享
- (void)initWithBtnWeixin {
    if (!_btnWeixin) {
        UIImage *image = [UIImage imageNamed:@"ic_share_weixin"];
        NSString *title = @"微信";
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        CGRect frame = CGRectMake(20, self.titleLabel.bottom + 10, 54, 54+24);
        _btnWeixin = [TYZImageTitleButton createWithImageButton:self frame:frame font:font target:self action:@selector(actionWithShare:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kBlackColor forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleTop imageTitleSpace:2];
            button.tag = 100;
        }];
    }
}

/// 朋友圈分享
- (void)initWithBtnCircle {
    if (!_btnCircle) {
        UIImage *image = [UIImage imageNamed:@"ic_share_wx_circle"];
        NSString *title = @"朋友圈";
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        CGRect frame = self.btnWeixin.frame;
        frame.origin.x = self.btnWeixin.right + 16;
        _btnCircle = [TYZImageTitleButton createWithImageButton:self frame:frame font:font target:self action:@selector(actionWithShare:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kBlackColor forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleTop imageTitleSpace:2];
            button.tag = 101;
        }];
    }
}

/// 取消
- (void)initWithBtnCancel {
    if (!_btnCancel) {
        CGRect frame = CGRectMake(0, self.height - IPHONEX_BOTTOM_SPACE - 40, self.width, 40);
        _btnCancel = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium] target:self action:@selector(clickedCancel:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"取消" forState:UIControlStateNormal];
            [button setTitleColor:kBlackColor forState:UIControlStateNormal];
        }];
    }
}

/// 分享
/// - Parameter sender: sender description
- (void)actionWithShare:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if (self.completionBlock)
    {
        self.completionBlock(@(btn.tag));
    }
}

- (void)showWithTitle:(NSString *)title data:(id)data completion:(void (^ _Nullable)(id _Nonnull))completionBlock failed:(void (^ _Nullable)(void))failedBlock
{
    self.title = title;
    self.completionBlock = completionBlock;
    self.failedBlock = failedBlock;
    if (!kCSKeyWindow.shareInfoView)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.shareInfoMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.shareInfoView = self;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedSubmit:)];
//        [maskView addGestureRecognizer:tap];
//        [self addGestureRecognizer:tap];
        
        [UIView animateWithDuration:0.2 animations:^{
            maskView.alpha = 0.4;
//            maskView.alpha = 0;
            self.alpha = 1;
        }];
    }
    [self initWithTitleLabel];

}

- (void)clickedCancel:(id)sender
{
    if (self.failedBlock)
    {
        self.failedBlock();
    }
}

- (void)clickedSubmit:(id)sender
{
    if (self.completionBlock)
    {
        self.completionBlock(nil);
    }
}

- (void)hide
{
    if (kCSKeyWindow.shareInfoView)
    {
        @weakify(self);
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            self.alpha = 0;
            kCSKeyWindow.shareInfoMaskView.alpha = 0;
        } completion:^(BOOL finished) {
//            @strongify(self);
//            if (self.completionBlock)
//            {
//                self.completionBlock(nil);
//            }
            [kCSKeyWindow.shareInfoView removeFromSuperview];
            [kCSKeyWindow.shareInfoMaskView removeFromSuperview];
            kCSKeyWindow.shareInfoMaskView = nil;
            kCSKeyWindow.shareInfoView = nil;
        }];
    }
}

@end
